package cn.xiaozhi0802.test;

import cn.xiaozhi0802.bean.User;
import cn.xiaozhi0802.bean.Users;
import cn.xiaozhi0802.dao.IUserDao;
import cn.xiaozhi0802.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test1 {
    @Test
    public void test5() throws IOException {
        //获取核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作
        sqlSession.delete("userMapper.delete",4);
        List<User> userList = sqlSession.selectList("userMapper.findAll");
        //mybatis执行更新操作 提交事务
        sqlSession.commit();
        //打印
        System.out.println(userList);
//      释放资源
        sqlSession.close();
    }
    @Test
    public void test4() throws IOException {
        //模拟User
        User user = new User();
        user.setWxy_id(4);
        user.setWxy_username("tom2");
        user.setWxy_password("abc");
        //获取核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作
        int i = sqlSession.update("userMapper.update", user);
        List<User> userList = sqlSession.selectList("userMapper.findAll");
        //mybatis执行更新操作 提交事务
        sqlSession.commit();
        //打印
        System.out.println(i);
        System.out.println(userList);
//      释放资源
        sqlSession.close();
    }
    @Test
    public void test3() throws IOException {
        //模拟User
        User user = new User();
        user.setWxy_username("tom");
        user.setWxy_password("abc");
        //获取核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作
        int i = sqlSession.insert("userMapper.save", user);
        List<User> userList = sqlSession.selectList("userMapper.findAll");
        //mybatis执行更新操作 提交事务
        sqlSession.commit();
        //打印
        System.out.println(i);
        System.out.println(userList);
//      释放资源
        sqlSession.close();
    }
    @Test
    public void test2() throws IOException {
        //获取核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作
        List<User> userList = sqlSession.selectList("userMapper.findAll");
        //打印
        System.out.println(userList);
//      释放资源
        sqlSession.close();
    }

    /**
     * mybatis的初体验
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        //读取配置文件
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        //创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //使用工厂生产sqlSession对象
        SqlSession session = factory.openSession();
        //使用sqlSession创建Dao接口的代理对象
        IUserDao userDao = session.getMapper(IUserDao.class);
        //使用代理对象执行方法
        //List<Users> usersList = userDao.findAll();
        //注解方式
        List<Users> usersList = userDao.findAlls();
        for (Users user : usersList) {
            System.out.println(user);
        }
        //释放资源
        session.close();
        in.close();
    }
}
